import time

from selenium import webdriver
from selenium.webdriver.common.by import By


class TestCase:
    def __init__(self):
        self.driver = webdriver.Chrome()
        # 进入ecshop
        self.driver.get('https://ecshop.test2.shopex123.com/')

    def seach(self, keyword):
        # 点击首页
        first_page = self.driver.find_element(By.LINK_TEXT, '首页')
        first_page.click()

        # 获取搜索框输入关键字
        time.sleep(2)
        name = self.driver.find_element(By.NAME, 'keywords')
        name.send_keys(keyword)

        # 点击搜索按钮
        time.sleep(3)
        name = self.driver.find_element(By.NAME, 'imageField')
        name.click()

    def register(self, username, email, passwd, confirm_passwd):
        # 点击[免费注册]
        free_register = self.driver.find_element(By.XPATH, '//*[@id="ECS_MEMBERZONE"]/a[2]')
        free_register.click()

        # 输入未注册过的用户名
        time.sleep(3)
        self.driver.find_element(By.ID, 'username').send_keys(username)

        # 输入正确格式的邮件地址
        time.sleep(3)
        self.driver.find_element(By.ID, 'email').send_keys(email)

        # 输入不少于6位数密码
        time.sleep(3)
        self.driver.find_element(By.ID, 'password1').send_keys(passwd)

        # 再次确定输入密码
        time.sleep(3)
        self.driver.find_element(By.ID, 'confirm_password').send_keys(confirm_passwd)

        # 点击[同意协议并注册按钮]
        time.sleep(3)
        submit = self.driver.find_element(By.NAME, 'Submit')
        submit.click()

        # 退出当前页面
        quit_page = self.driver.find_element(By.LINK_TEXT, '退出')
        quit_page.click()

    def login(self, username, passwd):
        # 点击[请登录]
        log = self.driver.find_element(By.XPATH, '//*[@id="ECS_MEMBERZONE"]/a[1]')
        log.click()

        # 输入已经注册正确用户名
        self.driver.find_element(By.NAME, 'username').send_keys(username)

        # 输入对应正确密码
        self.driver.find_element(By.NAME, 'password').send_keys(passwd)

        remember = self.driver.find_element(By.NAME, 'remember')
        remember.click()

        submit = self.driver.find_element(By.NAME, 'submit')
        submit.click()

    def quit(self):
        time.sleep(3)
        self.driver.quit()


if __name__ == '__main__':
    case = TestCase()
    case.seach('面部')
    case.register('admin34', '16324567@qq.com', '123456', '123456')
    case.login('username', 'passwd')
    case.quit()
